//
//  RootViewController.m
//  HTP
//
//  Created by admin on 15/10/26.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "RootViewController.h"
#import "SettingMainViewController.h"
#import "ClassCouresTimeViewController.h"
#import "HomeMianViewController.h"
#import "SelectSubViewController.h"
#import "YHButton.h"
#import "AppDelegate.h"
#define CHILDVIEW self.showViewController.view
#define CHILDCONTROLLER self.showViewController

typedef NS_ENUM(NSInteger ,RootSelectIndex){
    IndexOne = 0,
    IndexTwo = 1
};
@interface RootViewController ()<YHButtondelegate>
@property (nonatomic,retain) UIImageView *tabbarImage;
//自定义tabbar 展示的controller
@property (nonatomic,strong) UIViewController *showViewController;
//当前选择的viewController 下标
@property (nonatomic,assign) NSInteger selectIndex;

@property (nonatomic,assign) float topHeight;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  6.0下初始化会出现导航栏往下面偏移
     */
    if (!ISMore(7.0)) {
        _topHeight = -20;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(GotoNext:)
                                                 name:ROOTGOTONEXTCONTROLLER object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeProfile:)
                                                 name:CHANGEPROFILE object:nil];
    [self initTabbar];
    [self setChildViewControllers];
    [self steupShowController:0];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //设置右划菜单出现有效
    AppDelegate *tempAppDelegate = SELFDELEGATE;
    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
    
    [self _initNav];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //设置右划菜单出现无效
    AppDelegate *tempAppDelegate = SELFDELEGATE;
    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
}
//TODO:定义导航栏
/**
 *  leftBaritem --- yunhe
 */
-(void)_initNav{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBG"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"clearBg"]];
    [self addCustomTitleWithTitle:@"青苗家教"];
    UIBarButtonItem *negativeSpacer_1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                      target:nil action:nil];
    negativeSpacer_1.width = -10;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer_1, [[UIBarButtonItem alloc] initWithCustomView:[self leftNavImage]]];
}
/**
 *  yunhe 头像按钮自定义
 */
-(UIView *)leftNavImage{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,2,50,40)];
    UIImageView *headImage_1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    headImage_1.layer.masksToBounds = YES;
    headImage_1.layer.cornerRadius = 20.0;
    headImage_1.image = [UIImage imageNamed:@"headimage"];
    NSDictionary *userdic = [USERCONTENT getDefaultUser];
   [headImage_1 setNormalImageUrl:userdic[@"userPhoto"] anplaceholder:@"m_user_y_h"];
    [view addSubview:headImage_1];
    
    UIImageView *rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(43, 7.5, 10, 25)];
    rightImg.image = [UIImage imageNamed:@"hengxian"];
    [view addSubview:rightImg];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,2,50,40)];
    [view addSubview:button];
    [button addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    
    return view;
}
//TODO:菜单栏显示
- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}

//TODO:tabbar 视图切换
/**
 *  YHButton delegate
 *
 *  @param button  获取tag 标识展示那个viewcontroller
 
 *  @param groupId 那个组别
 */
-(void)didSelectYHButton:(YHButton *)button groupId:(NSString *)groupId{
//    [self steupShowController:button.tag];
    if (IndexOne == button.tag) {
        SelectSubViewController *selectSubVc = [[SelectSubViewController alloc] init];
        [self.navigationController pushViewController:selectSubVc animated:YES];
    }else{
        ClassCouresTimeViewController *classCourse = [[ClassCouresTimeViewController alloc] init];
        if (![USERCONTENT isLoginUser]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:GETUSERLOGIN object:classCourse];
            return;
        }
        [self.navigationController pushViewController:classCourse animated:YES];
    }
}
/**
 *  初始化添加tabbar视图
 */
-(void)setChildViewControllers{
    _selectIndex = 0;
    [self addChildViewController:[[HomeMianViewController alloc] init]];
    [self addChildViewController:[[ClassCouresTimeViewController alloc] init]];
}
/**
 *  切换视图
 *
 *  @param index 当前需要展示视图的下标
 */
-(void)steupShowController:(NSInteger)index{
    if (self.childViewControllers.count == 0) {
        return;
    }
    if ([CHILDVIEW superview]) {
        [CHILDVIEW removeFromSuperview];
    }
    CHILDCONTROLLER = [self.childViewControllers objectAtIndex:index];
    [self.view addSubview:CHILDVIEW];
    [CHILDVIEW mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_tabbarImage.top);
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.top.equalTo(_topHeight);
        //6.0适配
        if (_topHeight == -20) {
            _topHeight = 0;
        }
    }];
    [self.view bringSubviewToFront:_tabbarImage];
}
//TODO:初始化tabbar
-(void)initTabbar{
    _tabbarImage = [[UIImageView alloc] init];
    _tabbarImage.backgroundColor = [UIColor whiteColor];
    _tabbarImage.userInteractionEnabled = YES;
    [self.view addSubview:_tabbarImage];

    [_tabbarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.bottom);
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.height.equalTo(49);
    }];
    
    YHButton *yh_1 = [[YHButton alloc] initWithDelegate:self groupId:@"YH" buttonImage:@"yuyue" unSelectImage:@"yuyue" title:@"预约家教" withFrame:CGRectMake(0, 0, ScreenWidth / 2.0-0.5, 49)];
    yh_1.tag = IndexOne;
    [self.tabbarImage addSubview:yh_1];
    
    YHButton *yh_2 = [[YHButton alloc] initWithDelegate:self groupId:@"YH" buttonImage:@"course" unSelectImage:@"course" title:@"我的课程" withFrame:CGRectMake(ScreenWidth / 2.0+1, 0, ScreenWidth / 2.0-0.5, 49)];
    yh_2.tag = IndexTwo;
    [self.tabbarImage addSubview:yh_2];
    
    UIImageView *s_line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth / 2.0-0.5, 4.5, 1, 40)];
    s_line.image = [UIImage imageNamed:@""];
    s_line.backgroundColor = RGBCOLOR(179, 179, 179, 1);
    [_tabbarImage addSubview:s_line];
}
//TODO:接受通知事件
/**
 *  root跳转
 *
 *  @param Notification 里面包含需要跳转的controller
 */
-(void)GotoNext:(NSNotification *)Notification{
   
    BaseViewController *base = (BaseViewController *)Notification.object;
    if (![USERCONTENT isLoginUser]) {
        if ([base isKindOfClass:[SettingMainViewController class]]) {
            
        }else{
            [[NSNotificationCenter defaultCenter] postNotificationName:GETUSERLOGIN object:base];
            return;
        }
        
    }
    [self.navigationController pushViewController:base animated:NO];
}
/**
 *  个人信息修改通知
 *
 *  @param Notification
 */
-(void)changeProfile:(NSNotification *)Notification{
//    DLog(@"个人信息修改");
//    //Notification.object
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
